package helper
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	public class Services 
	{
		//[Embed (source = "../assets/TechComBank2.swf", mimeType = "application/octet-stream")]
		private var _tech_com_bank:Class;
		private const GET_RESOURCE_URL:String = "http://ant.cami.vn/techcombank/game/getResource"
		private const GET_GIFTS_URL:String ="http://ant.cami.vn/techcombank/game/getGifts ";
		private const GET_CARDS_URL:String ="http://ant.cami.vn/techcombank/game/getCards";
		private const UPDATE_SCORE_URL:String="http://ant.cami.vn/techcombank/game/updateScore1/";
		private const GET_TOP_URL:String = "http://ant.cami.vn/techcombank/game/getTop";
		private const GET_REWARD_URL:String = "http://ant.cami.vn/techcombank/game/getReward";
		private var _get_gift_loader:URLLoader;
		private var _get_card_loader:URLLoader;
		private var _update_score:URLLoader;
		private var _get_top_loader:URLLoader;
		private var _get_resource_loader:URLLoader;
		private var _get_reward_loader:URLLoader;
		private var _tech_loader:Loader;
		public static var instance:Services;
		public function Services()
		{
			if(!instance){
				instance = this;
			}
			init();
		}
		public static function getInstance():Services{
			if(!instance){
				instance = new Services();
			}
			return instance;
		}
		private function init():void{
			_get_gift_loader = new URLLoader();
			_get_card_loader = new URLLoader();
			_update_score = new URLLoader();
			_get_top_loader = new URLLoader();
			_get_resource_loader = new URLLoader();
			_get_reward_loader = new URLLoader();
			
			_tech_loader = new Loader();
			_tech_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadTechComplete)
			_tech_loader.loadBytes(new _tech_com_bank());
			
		}
		private function loadTechComplete(e:Event):void{
			_tech_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadTechComplete);
			var sercurity:Class =  _tech_loader.contentLoaderInfo.applicationDomain.getDefinition("Sercurity") as Class;
			var tech:Object = new sercurity();
			//trace("pass pass:",tech.pass);
			//_p_loader = null;
			//_picasa_cl = new p_cl();
			
		}
		public function getGifts(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new  URLRequest(GET_GIFTS_URL);
			_get_gift_loader.addEventListener(Event.COMPLETE,result_f);
			_get_gift_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_gift_loader.load(url_request);
		}
		public function getCards(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new URLRequest(GET_CARDS_URL);
			_get_card_loader.addEventListener(Event.COMPLETE,result_f);
			_get_card_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_card_loader.load(url_request);
		}
		public function updateScore(result_f:Function,error_f:Function,variable:String):void{
			var url_request:URLRequest = new URLRequest(UPDATE_SCORE_URL + variable);
			_update_score.addEventListener(Event.COMPLETE,result_f);
			_update_score.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_update_score.load(url_request);
		}
		public function getTop(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new URLRequest(GET_TOP_URL);
			_get_top_loader.addEventListener(Event.COMPLETE,result_f);
			_get_top_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_top_loader.load(url_request);
		}
		public function getResource(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new URLRequest(GET_RESOURCE_URL);
			_get_resource_loader.addEventListener(Event.COMPLETE,result_f);
			_get_resource_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_resource_loader.load(url_request);
		}
		public function getReward(result_f:Function,error_f:Function):void{
			var url_request:URLRequest = new URLRequest(GET_REWARD_URL);
			_get_reward_loader.addEventListener(Event.COMPLETE,result_f);
			_get_reward_loader.addEventListener(IOErrorEvent.IO_ERROR,error_f);
			_get_reward_loader.load(url_request);
		}
		
	}
}